package com.kgc.scd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kgc.scd.bean.ShopCarousel;
import com.kgc.scd.util.PageSupport;
import com.kgc.scd.vo.ShopCarouselVO;
import com.kgc.scd.bean.ShopComment;
import com.kgc.scd.bean.ShopGoods;
import com.kgc.scd.mapper.CarouselMapper;
import com.kgc.scd.mapper.CommentMapper;
import com.kgc.scd.mapper.GoodsMapper;
import com.kgc.scd.service.GoodsInfoCommentService;
import com.kgc.scd.vo.ShopCommentVO;
import com.kgc.scd.vo.ShopGoodsVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Created On : 2022/11/14.
 * <p>
 * Author : 22166
 * <p>
 * Description: 查询商品评论业务实现类
 */
@Service
@Slf4j
public class GoodsInfoCommentServiceInfoImpl implements GoodsInfoCommentService {


    @Autowired(required = false)
    private CommentMapper commentMapper;

    @Autowired(required = false)
    private GoodsMapper goodsMapper;

    @Autowired(required = false)
    private CarouselMapper carouselMapper;

    @Override
    public ShopGoodsVO selectGoodsInfo(String goodsId) {
        //创建显示层vo实体,将查询结果实体,拷贝属性
        ShopGoodsVO shopGoodsVO = new ShopGoodsVO();

        //调用持久层接口,拷贝属性到vo实体
        ShopGoods shopGoods = goodsMapper.selectById(goodsId);
        BeanUtils.copyProperties(shopGoods,shopGoodsVO);

        //调用持久层接口,查询商品详情
        return shopGoodsVO;
    }

    @Override
    public int getShoppingCommentSum(String goodsId) {

        QueryWrapper<ShopComment> shopCommentQueryWrapper = new QueryWrapper<>();
        shopCommentQueryWrapper.eq("goods_id",goodsId);

        int count = commentMapper.selectCount(shopCommentQueryWrapper).intValue();

        return count;
    }


    @Override
    public PageSupport<ShopCommentVO> getCommentListPage(String goodsId, Integer pageNo, Integer pageSize) {

        // 创建统一返回分页对象
        PageSupport<ShopCommentVO> pageSupport = new PageSupport<>();

        // mybatis-plus，创建一个查询对象，提供好的，直接用
        QueryWrapper<ShopComment> shopCommentQueryWrapper = new QueryWrapper<>();
        shopCommentQueryWrapper.likeRight("goods_id",goodsId);
        shopCommentQueryWrapper.orderByDesc("creation_time_content");

        //创建分页对象
        Page<ShopComment> shopCommentPage = new Page<>(pageNo, pageSize);

        // 查询评论列表
        Page<ShopComment> commentResultPage = commentMapper.selectPage(shopCommentPage, shopCommentQueryWrapper);

        // 处理返回的结果列表，将状态进行转换为描述
        List<ShopCommentVO> shopCommentVOList = commentResultPage.getRecords().stream().map(comment -> {
            // 创建VO实体对象
            ShopCommentVO shopCommentVO = new ShopCommentVO();
            // 拷贝实体属性
            BeanUtils.copyProperties(comment, shopCommentVO);

            return shopCommentVO;
        }).collect(Collectors.toList());


        // 添加分页的页码数据
        pageSupport.setPageNo(pageNo);
        pageSupport.setPageSize(pageSize);
        pageSupport.setTotalPage((int) commentResultPage.getPages());
        pageSupport.setTotalCount((int) commentResultPage.getTotal());
        pageSupport.setPageData(shopCommentVOList);

        log.info("11111111111{}",pageSupport);
        log.info("------ 总页数：{}，总条数：{}，当前页：{}，条数：{} ------", commentResultPage.getPages(), commentResultPage.getTotal(), commentResultPage.getCurrent(), commentResultPage.getSize());

        // 返回结果集合
        return pageSupport;
    }

    @Override
    public List<ShopCarouselVO> queryAll() {


        // 处理返回的结果列表，将状态进行转换为描述
        List<ShopCarouselVO> shopCarouselVOList = carouselMapper.selectList(null).stream().map(carousel -> {
            // 创建VO实体对象
            ShopCarouselVO shopCarouselVO = new ShopCarouselVO();
            // 拷贝实体属性
            BeanUtils.copyProperties(carousel, shopCarouselVO);

            return shopCarouselVO;
        }).collect(Collectors.toList());

        // 返回结果集合
        return shopCarouselVOList;
    }

    @Override
    public int updateShopCarousel(String id, String goodsId) {

        UpdateWrapper<ShopCarousel> updateWrapper = new UpdateWrapper<>();

        updateWrapper.set("goods_url","http://localhost:8099/shopCommentList?goodsId="+goodsId+"&pageNo=1&pageSize=3").set("goods_id",goodsId).eq("id",id);

        return carouselMapper.update(null,updateWrapper);
    }


}